package edu.utn.tacs.grupouno.negocio;

import java.util.Calendar;
import java.util.Date;

import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class TurnoSemanal {
	private Long id;
	
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY, column = "id")
	@Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
	@XmlAttribute(name="key")
	private String encodedKey;
	
	@NotPersistent
	@XmlElement
	private Dia.DiaDeLaSemana diaDeLaSemana;
	
	@Persistent
	@XmlElement
	private String horario;
	
	@Persistent
	@XmlElement
	private Date fechaInicio;
	
	public void setDiaDeLaSemanaAsString(String diaDeLaSemana) {
		if(diaDeLaSemana == null || diaDeLaSemana.equals("null")) {
			this.diaDeLaSemana = null;
		} else {
			this.diaDeLaSemana = Dia.DiaDeLaSemana.valueOf(diaDeLaSemana);
		}
	}
	
	public void setDiaDeLaSemana(Dia.DiaDeLaSemana diaDeLaSemana) {
		this.diaDeLaSemana = diaDeLaSemana;
	}
	public Dia.DiaDeLaSemana getDiaDeLaSemana() {
		return diaDeLaSemana;
	}
	@XmlTransient
	@Persistent(name="diaDeLaSemana")
	public String getDiaDeLaSemanaAsString() {
		return diaDeLaSemana != null ? diaDeLaSemana.toString() : "null";
	}
	public void setHorario(String horario) {
		this.horario = horario;
	}
	public String getHorario() {
		return horario;
	}
	public void setFechaInicio(Date fechaInicio) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(fechaInicio);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		this.fechaInicio = cal.getTime();
	}
	public Date getFechaInicio() {
		return fechaInicio;
	}

	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}

	public void setEncodedKey(String encodedKey) {
		this.encodedKey = encodedKey;
	}
	@XmlTransient
	public String getEncodedKey() {
		return encodedKey;
	}
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder("{\n");
		sb.append("\t\tdiaDeLaSemana: '").append(diaDeLaSemana).append("',\n");
		sb.append("\t\thorario: '").append(horario).append("',\n");
		sb.append("\t\tfechaInicio: '").append(fechaInicio).append("',\n");
		sb.append("\t}");
		return sb.toString();
	}
}
